package com.punai.monthanalysis.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.multi.ListValueMap;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punai.common.core.domain.BaseEntity;
import com.punai.common.utils.NumberUtil;
import com.punai.externalccb.constants.CCBConstants;
import com.punai.externalccb.domain.EcoCbinfo;
import com.punai.externalccb.domain.YdCbfeeiteminfoStandard;
import com.punai.externalccb.domain.YdSummary;
import com.punai.externalccb.mapper.EcoCbinfoMapper;
import com.punai.externalccb.mapper.YdSummaryMapper;
import com.punai.monthanalysis.bo.ProYdcbfxDwgccsffxbBo;
import com.punai.monthanalysis.domain.ProYdcbfxDwgccsffxb;
import com.punai.monthanalysis.mapper.ProYdcbfxDwgccsffxbMapper;
import com.punai.monthanalysis.service.IProYdcbfxDwgccsffxbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 单位工程措施费分析Service业务层处理
 *
 * @author zxw_
 * @date 2023-04-19
 */
@Service
public class ProYdcbfxDwgccsffxbServiceImpl extends ServiceImpl<ProYdcbfxDwgccsffxbMapper,ProYdcbfxDwgccsffxb> implements IProYdcbfxDwgccsffxbService
{
    @Autowired
    private ProYdcbfxDwgccsffxbMapper proYdcbfxDwgccsffxbMapper;
    @Autowired
    private EcoCbinfoMapper ecoCbinfoMapper;
    @Autowired
    private YdSummaryMapper ydSummaryMapper;

    @Override
    public List<? extends BaseEntity> getTabsDataList(String proId,String monthId,List<YdCbfeeiteminfoStandard> list,List<YdCbfeeiteminfoStandard> categoryList) {
        //总建筑面积
        List<EcoCbinfo> ecoCbinfos = ecoCbinfoMapper.selectList(Wrappers.<EcoCbinfo>lambdaQuery()
                .eq(EcoCbinfo::getName, "总建筑面积(m2)")
                .eq(EcoCbinfo::getProId, proId)
                .eq(EcoCbinfo::getBidnodeid, 1)
                .eq(EcoCbinfo::getStagetype, CCBConstants.CCB_TYPE.YDCBFX.getCode()));
        BigDecimal zjzmj = ecoCbinfos.stream().map(EcoCbinfo::getContent).filter(Objects::nonNull).map(BigDecimal::new).reduce(BigDecimal.ZERO, BigDecimal::add);
        //盘点总措施费
        List<YdSummary> ydSummaryList = ydSummaryMapper.selectList(Wrappers.<YdSummary>lambdaQuery()
                .eq(YdSummary::getName,"其他措施费")
                .eq(YdSummary::getPid,0)
                .eq(YdSummary::getProId, proId)
                .eq(YdSummary::getYdDate, monthId)
                .eq(YdSummary::getStagetype, CCBConstants.CCB_TYPE.YDCBFX.getCode()));
        BigDecimal ysZcsf = ydSummaryList.stream().map(YdSummary::getCostsCz).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);

        ListValueMap<String,YdCbfeeiteminfoStandard> listValueMap = new ListValueMap<>();
        list.forEach(ydCbfeeiteminfoStandard -> {
            listValueMap.putValue(ydCbfeeiteminfoStandard.getOpid()+"",ydCbfeeiteminfoStandard);
        });
        List<YdCbfeeiteminfoStandard> roots = listValueMap.get("0");

        List<ProYdcbfxDwgccsffxbBo> result = new ArrayList<>();
        if (CollUtil.isNotEmpty(roots)){
            for (YdCbfeeiteminfoStandard root : roots) {
                ProYdcbfxDwgccsffxbBo convert = convert(root,zjzmj,ysZcsf);
                result.add(convert);
                setChildren(convert,listValueMap,zjzmj,ysZcsf);
            }
        }
        return result;
    }

    private void setChildren(ProYdcbfxDwgccsffxbBo parent,
                             ListValueMap<String, YdCbfeeiteminfoStandard> listValueMap,BigDecimal zjzmj,BigDecimal ysZcsf) {

        String id = parent.getId();
        List<YdCbfeeiteminfoStandard> children = listValueMap.get(id);
        if (CollUtil.isNotEmpty(children)) {
            for (YdCbfeeiteminfoStandard child : children) {
                ProYdcbfxDwgccsffxbBo convert = convert(child,zjzmj,ysZcsf);
                parent.getChildren().add(convert);
                setChildren(convert,listValueMap,zjzmj,ysZcsf);
            }
        }
    }


    private ProYdcbfxDwgccsffxbBo convert(YdCbfeeiteminfoStandard in,BigDecimal zjzmj,BigDecimal ysZcsf) {
        ProYdcbfxDwgccsffxbBo out = new ProYdcbfxDwgccsffxbBo();
        out.setId(in.getOid()+"");
        out.setItemName(in.getName());
        out.setYsQuantity(in.getYsAmount());
        out.setYsPrice(in.getYsTotal());
        out.setSjQuantity(in.getActualCostAmout());
        out.setSjPrice(in.getActualCostNoTaxTotal());
        out.setPmdj(NumberUtil.divide(in.getActualCostNoTaxTotal(),zjzmj)); //实际金额 / 建筑面积
        out.setZczbl(NumberUtil.divide(out.getSjPrice(),2,in.getKlCzTotal())); //实际金额 / 截止本月累计产值
        out.setZcsfbl(NumberUtil.divide(in.getActualCostNoTaxTotal(),2,ysZcsf));// 实际金额 / 盘点总措施费
        out.setRemark("");
        return out;
    }


}
